package com.lancoo.lancooimageloader;

import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.lancoo.lancooimageloader.glide.GlideOptionConverter;
import com.lancoo.lancooimageloader.xutils3.xUtilsOptionConverter;

/**
 * Author: Andecy
 * Time: 2017/5/11
 * Email: andecy@foxmail.com
 * Description: TODO
 */

public class LancooImageOptions {
    private OptionConverter converter;

    // region ###################### decode options (equals & hashcode prop) ################
    private int width = 0; // 小于0时不采样压缩. 等于0时自动识别ImageView的宽高和maxWidth.
    private int height = 0; // 小于0时不采样压缩. 等于0时自动识别ImageView的宽高和maxHeight.
    private boolean ignoreGif = true;
    private boolean circular = false;
    private boolean useMemCache;

    // region ############# display options
    private int loadingDrawableId;
    private int failureDrawableId;
    private Drawable loadingDrawable;
    private Drawable failureDrawable;

    private ImageView.ScaleType placeholderScaleType;
    private ImageView.ScaleType imageScaleType;

    private LancooImageOptions() {
        this.placeholderScaleType = ImageView.ScaleType.CENTER_INSIDE;
        this.imageScaleType = ImageView.ScaleType.CENTER_CROP;
        this.useMemCache = true;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getLoadingDrawableId() {
        return loadingDrawableId;
    }

    public int getFailureDrawableId() {
        return failureDrawableId;
    }

    public Drawable getLoadingDrawable() {
        return loadingDrawable;
    }

    public Drawable getFailureDrawable() {
        return failureDrawable;
    }

    public boolean isIgnoreGif() {
        return ignoreGif;
    }

    public boolean isCircular() {
        return circular;
    }

    public boolean isUseMemCache() {
        return useMemCache;
    }

    public ImageView.ScaleType getPlaceholderScaleType() {
        return this.placeholderScaleType;
    }

    public ImageView.ScaleType getImageScaleType() {
        return this.imageScaleType;
    }

    public OptionConverter getConverter() {
        return converter;
    }

    public static class Builder {

        protected LancooImageOptions options;


        public Builder() {
            newImageOptions();
        }


        protected void newImageOptions() {
            options = new LancooImageOptions();
        }


        public LancooImageOptions build() {
            return  buildByxUtils();
        }

        public LancooImageOptions buildByxUtils() {
            if (options.converter == null) {
                options.converter = new xUtilsOptionConverter(options);
            }
            return options;
        }

        public LancooImageOptions buildByGlide() {
            if (options.converter == null) {
                options.converter = new GlideOptionConverter(options);
            }
            return options;
        }
        /**
         * 小于0时不采样压缩. 等于0时自动识别ImageView的宽高和(maxWidth, maxHeight).
         *
         * @param width  图片宽度
         * @param height 图片高度
         * @return builder
         */
        public Builder setSize(int width, int height) {
            options.width = width;
            options.height = height;
            return this;
        }

        /**
         * 设置是否忽略gif图片，默认为true
         *
         * @param ignoreGif 是否忽略gif图
         * @return builder
         */
        public Builder setIgnoreGif(boolean ignoreGif) {
            options.ignoreGif = ignoreGif;
            return this;
        }

        /**
         * 设置加载中的图片
         *
         * @param loadingDrawableId 图片的资源id
         * @return builder
         */
        public Builder setLoadingDrawable(int loadingDrawableId) {
            this.options.loadingDrawableId = loadingDrawableId;
            return this;
        }

        /**
         * 设置加载中的图片
         *
         * @param loadingDrawable drawable
         * @return builder
         */
        public Builder setLoadingDrawable(Drawable loadingDrawable) {
            this.options.loadingDrawable = loadingDrawable;
            return this;
        }

        /**
         * 设置加载失败的图片
         *
         * @param failureDrawableId 图片的资源id
         * @return builder
         */
        public Builder setFailureDrawable(int failureDrawableId) {
            this.options.failureDrawableId = failureDrawableId;
            return this;
        }

        /**
         * 设置加载失败的图片
         *
         * @param failureDrawable drawable
         * @return builder
         */
        public Builder setFailureDrawable(Drawable failureDrawable) {
            this.options.failureDrawable = failureDrawable;
            return this;
        }

        /**
         * 设置图片是否显示为圆形
         *
         * @param circular 是否显示为圆形
         * @return builder
         */
        public Builder setCircular(boolean circular) {
            this.options.circular = circular;
            return this;
        }

        /**
         * 设置是否使用缓存的图片
         *
         * @param useMemCache 是否使用缓存的图片
         * @return builder
         */
        public Builder setUseMemCache(boolean useMemCache) {
            this.options.useMemCache = useMemCache;
            return this;
        }

        /**
         * 设置占位图的ScaleType
         *
         * @param placeholderScaleType ScaleType
         * @return builder
         */
        public Builder setPlaceholderScaleType(ImageView.ScaleType placeholderScaleType) {
            this.options.placeholderScaleType = placeholderScaleType;
            return this;
        }

        /**
         * 设置加载成功后图片的ScaleType
         *
         * @param imageScaleType ScaleType
         * @return builder
         */
        public Builder setImageScaleType(ImageView.ScaleType imageScaleType) {
            this.options.imageScaleType = imageScaleType;
            return this;
        }
    }


}
